<?php
App::uses('AppController', 'Controller');
class QuizzesController extends AppController {

	var $name = 'Quizzes';
	
	public $paginate = array(
        'order' => array(
            'Quiz.group' => 'asc', 'Quiz.institution' => 'asc'
        )
    );
	function index() {
		$this->Quiz->recursive = 0;
		if(empty($this->data)){
			$this->set('quizzes', $this->paginate());

		}

			else{
				$input = $this->data['Quiz']['input'];
				$filtro = $this->data['Quiz']['filtro'];
				 switch ($filtro) {
						case 'Nombre_Completo':
							$this->set('quizzes', $this->paginate(array("or" => array('Nombre_Completo' => $input, 'Nombre_Completo LIKE' => '%'.$input.'%'))));
							break;
						case 'institution':
							$this->set('quizzes', $this->paginate(array("or" => array('institution' => $input, 'institution LIKE' => '%'.$input.'%'))));
							break;
				}
		  }
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid quiz', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('quiz', $this->Quiz->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Quiz->create();
			if ($this->Quiz->save($this->data)) {
				$this->Session->setFlash(__('The quiz has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The quiz could not be saved. Please, try again.', true));
			}
		}
		$questions = $this->Quiz->Question->find('list');
		$this->set(compact('questions'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid quiz', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Quiz->save($this->data)) {
				$this->Session->setFlash(__('The quiz has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The quiz could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Quiz->read(null, $id);
		}
		$questions = $this->Quiz->Question->find('list');
		$this->set(compact('questions'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for quiz', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Quiz->delete($id)) {
			$this->Session->setFlash(__('Quiz deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Quiz was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>
